In computer science, garbage collection is a form of automatic memory management that frees the programmer from manually dealing with memory de-allocation. In other words, garbage collection is a process that performs the automatic recycling of unused computer storage. A garbage collector performs garbage collection by reclaiming garbage, or memory occupied by objects that are unreachable or no longer in use by a computer program. An object refers to any data structure created by a computer program. If an object is referenced by or pointed to by a “strong pointer,” it is always protected from garbage collection.
Garbage collected languages also often have a notion of a “weak pointer.” In computer programming, a weak pointer is a type of a reference that does not protect the referenced object from collection by a garbage collector. An object that is only pointed to by a weak pointer is subject to garbage collection. If an object is pointed to by both a strong pointer and a weak pointer, the object is protected from garbage collection. Consequently, a weak pointer is a pointer that, unlike a strong pointer, does not protect the object it points to from garbage collection if it is the only pointer to the object. An object referenced only by a weak pointer or a weak reference is considered unreachable and may be collected by the garbage collector.
Weak pointers can be processed in time proportional to the number of weak pointers discovered by the garbage collector during its operation. A garbage collector collects all of the weak pointers and after all of the strong pointers have been visited, processes any remaining weak pointers which refer to unreachable objects in a single pass. In contrast, as previously discussed, a weak property points to two objects and thus, has two pointers, a pointer to a key object and a pointer to a value object. A weak property may be described as a mapping from an object called a “key” to an object called a “value.”
A weak property's pointer to a key object behaves like a weak pointer. However, a weak property preserves a reference to the value object only if the key object is also reachable. The pointer to the value object is therefore different from an ordinary weak pointer. A weak property is commonly used to associate information with an object but store it outside of the object. The “key” and “value” objects are permitted to be the same object though this is not commonly the case.
Furthermore, the pointer to the value object is considered weak if the key object is not pointed to by a non-weak pointer. Otherwise, the pointer to the value object may be considered a non-weak pointer or a strong pointer. In other words, a weak property may be defined as an association consisting of two pointers, a pointer to a key object and a pointer to a value object.
A weak property also has a much higher processing time complexity, often quadratic in comparison to the processing time complexity of a weak pointer. Thus, the processing of weak properties is more costly, often quadratic with respect to the number of weak properties traversed during garbage collection. Extensive use of weak properties can increase latency in garbage collection